package com.googlecode.maratische.google.model;

import java.sql.SQLException;
import java.util.GregorianCalendar;
import java.util.List;

import be.lechtitseb.google.reader.api.model.exception.GoogleReaderException;

import com.googlecode.maratische.google.BaseException;
import com.googlecode.maratische.google.DaoAction;

public class CommandSearchItemForDelete extends Command {

	@Override
	public String getType() {
		return Commands.SearchItemForDelete.toString();
	}
	
	@Override
	public Integer getPriority() {
		return 8;
	}
	
	@Override
	public boolean execute(DaoAction daoAction) throws BaseException, GoogleReaderException, SQLException {
		// поиск всех прочитанных и подлежащих удалению элементов
		GregorianCalendar calendar = new GregorianCalendar();
		/**
		 * TODO вынести в конфигурацию
		 */
		calendar.add(GregorianCalendar.DAY_OF_MONTH, -5);
		java.sql.Date date = new java.sql.Date(calendar.getTimeInMillis());
		List<Item> items = daoAction.getItemDao().findReadedForErase(date);
		if (items != null && items.size() > 0) {
			for (Item item : items) {
				if (item.getLabelId() != null) {
					continue;
				}
				CommandRemoveReadedItemMedia command = (CommandRemoveReadedItemMedia) Command
						.createCommand(Commands.RemoveReadedItemMedia);
				command.setInteger1(item.getId());
				daoAction.getCommandDao().save(command);
			}
		}
		return true;
	}

}
